define(function(require, exports, module) {
	var check = require('resource/js/component/check.js');
	exports.checkForm = function(obj, btnName, processDatas, approverList,fn) {
		if($(obj).hasClass("l-btn-disabled")){
			return;
		}
		$('#btns').children().addClass('l-btn-disabled');
		var main = {};
		var subs = {};
		var subsForm = {};
		var processDataParam = {};
		var processData = {};
		if (processDatas != "" && processDatas.length > 0) {
			processData = eval('(' + processDatas + ')');
			if (processData != null && processData.hasOwnProperty("param")) {
				processDataParam = processData.param;
			}
		}
		if($("#processNodeId").val() == 'PROCESS_START'){
			var hasStart = false;
			if(approverList && approverList.length > 0){
				for(var i=0;i<approverList.length;i++){
					if(approverList[i]==undefined){
						$(".error").text("请添加第"+(i+1)+"审批人").show().delay(2000).hide(0);
						$('#btns').children().removeClass('l-btn-disabled');
						return;
					}else{
						if(approverList[i].nodeId && approverList[i].nodeId ==="PROCESS_START"){
							hasStart = true;
						}
					}
				}
				if(!hasStart){
					var startUser = $("#currentUserId").val();
					approverList.push({nodeId:"PROCESS_START",orderNum:0,userType:"userId","userId":startUser.toString(),approveStatus:0});
				}	
			}else{		
				$(".error").text("请先设置审批人").show().delay(2000).hide(0);
				$('#btns').children().removeClass('l-btn-disabled');
				return;			
			}
		}
		if (chechInput($("#processForm").find(".textbox-text"), main)) {
			var hidden = $("#processForm").find("input").filter(".processHidden");
			$.each(hidden, function(i, input) {
				packageData($(input).attr("id"), $(input).val(), main);
			});
			var tabs = $("ul[sign='processTable']");
			var ok = true;
			if (tabs && tabs.length > 0) {
				ok = false;
				$.each(tabs, function(i, tab) {
					ok = checkTable($(tab).attr("name"), subs, tab);
					if (!ok) {
						return false;
					}
				});
			}
			if (ok) {
				ok = chechInput($(".subsForm").find(".textbox-text"), subsForm);
				if (ok) {
					hidden = $(".subsForm").find("input").filter(".processHidden");
					$.each(hidden, function(i, input) {
						packageData($(input).attr("id"), $(input).val(), subsForm);
					});
				}
				$.each(subsForm, function(key, val) {
					if (val.data) {
						var temp = [];
						temp.push(val.data);
						val.data = temp;
					}
				});
				$.extend(true, subs, subsForm)
				if (ok) {
					//流程设置js校验
					if (processData != null && processData.hasOwnProperty("submitJs")) {
						var js = processData.submitJs;
						if (js != null && js != "") {
							js = js.replace(/\^/g, "\"");
							js = js.replace(/\~/g, "'");
							var temp = eval("(" + js + ")");
							ok = temp(null, '1');
						}
					}
					if (ok) {
						//组合流程参数
						main = getProcessCols(main, "mainIsProcess");
						subs = getProcessCols(subs, "subIsProcess");
						//提交数据
						addPro(main);
						addPro(subs);
						var busenessId = "";
						if ($("#busenessId") && $("#busenessId").length > 0) {
							busenessId = $("#busenessId").val();
						}
						var ccUsers = '';
						if ($("#copyOfUsers").val()) {
							$.each(JSON.parse($("#copyOfUsers").val()), function(i, val) {
								ccUsers += val + ','
							});
							ccUsers = ccUsers.substr(0, ccUsers.length - 1);
						}
						var data = {
							main: main,
							subs: subs,
							processData: processDataParam,
							approverList:{approvers:approverList},
							ccUsers: {ccUsers: ccUsers},
							info: {
								key: $("#processKey").val(),
								processNodeId: $("#processNodeId").val(),
								processInstanceId: $("#processInstanceId").val(),
								taskId: $("#taskId").val(),
								busenessId: busenessId,
								action: btnName,
								assignUserId: $("#assign-UserId").val()
							}
						};
						if (typeof fn == 'function') {
							fn(data);
						} else {
							require.async('resource/js/component/ajax.js', function(ajax) {
								ajax.init().success(function(code, msg, data) {
										if (code == 0) {
											$(".error").text(btnName+"成功").show().delay(1000).hide(0);
											setTimeout(function() {
//												if ($("#taskId").val() == "") {
													return_prepage(2);
//												} else {
//													return_prepage();
//												}
//												if (window.js && window.js.isGoback) {
//													window.js.isGoback("false");
//												}
											}, 1000);
										} else {
											if (code == 11 && window.js && window.js.logout) {
												window.js.logout();
											}
											$('#btns').children().removeClass('l-btn-disabled');
											$(".error").text(msg).show().delay(1000).hide(0);
										}
									}).error(function(code, msg, datas) {
										$(".error").text(msg).show().delay(1000).hide(0);
										$('#btns').children().removeClass('l-btn-disabled');
									})
									.contentType('application/json').post(BASEPATH + '/oa/act/activitiMain/saveProcessData.htm', JSON.stringify(data));
							});
						}
					} else {
						setTimeout(function() {
							$('#btns').children().removeClass('l-btn-disabled');
						}, 1000);
					}
				} else {
					setTimeout(function() {
						$('#btns').children().removeClass('l-btn-disabled');
					}, 1000);
				}
			} else {
				setTimeout(function() {
					$('#btns').children().removeClass('l-btn-disabled');
				}, 1000);
			}
		} else {
			setTimeout(function() {
				$('#btns').children().removeClass('l-btn-disabled');
			}, 1000);
		}
	}

	exports.requireData = function(obj, btnName, processDatas) {
		if($(obj).hasClass("l-btn-disabled")){
			return;
		}
		var main = {};
		var subs = {};
		var subsForm = {};
		var processDataParam = {};
		var processData = {};
		if (processDatas != "" && processDatas.length > 0) {
			processData = eval('(' + processDatas + ')');
			if (processData != null && processData.hasOwnProperty("param")) {
				processDataParam = processData.param;
			}
		}

		if (chechInput($("#processForm").find(".textbox-text"), main)) {
			var hidden = $("#processForm").find("input").filter(".processHidden");
			$.each(hidden, function(i, input) {
				packageData($(input).attr("id"), $(input).val(), main);
			});
			var tabs = $("ul[sign='processTable']");
			var ok = true;
			if (tabs && tabs.length > 0) {
				ok = false;
				$.each(tabs, function(i, tab) {
					ok = checkTable($(tab).attr("name"), subs, tab);
					if (!ok) {
						return null;
					}
				});
			}

			if (ok) {
				ok = chechInput($(".subsForm").find(".textbox-text"), subsForm);
				if (ok) {
					hidden = $(".subsForm").find("input").filter(".processHidden");
					$.each(hidden, function(i, input) {
						packageData($(input).attr("id"), $(input).val(), subsForm);
					});
				}
				$.each(subsForm, function(key, val) {
					if (val.data) {
						var temp = [];
						temp.push(val.data);
						val.data = temp;
					}
				});
				$.extend(true, subs, subsForm)
				if (ok) {
					//流程设置js校验
					if (processData != null && processData.hasOwnProperty("submitJs")) {
						var js = processData.submitJs;
						if (js != null && js != "") {
							js = js.replace(/\^/g, "\"");
							js = js.replace(/\~/g, "'");
							var temp = eval("(" + js + ")");
							ok = temp(null, '1');
						}
					}

					if (ok) {
						//组合流程参数
						main = getProcessCols(main, "mainIsProcess");
						subs = getProcessCols(subs, "subIsProcess");
						//提交数据
						addPro(main);
						addPro(subs);
						var busenessId = "";
						if ($("#busenessId") && $("#busenessId").length > 0) {
							busenessId = $("#busenessId").val();
						}
						var ccUsers = '';
						if ($("#copyOfUsers").val()) {
							$.each(JSON.parse($("#copyOfUsers").val()), function(i, val) {
								ccUsers += val + ','
							});
							ccUsers = ccUsers.substr(0, ccUsers.length - 1);
						}
						var data = {
							main: main,
							subs: subs,
							processData: processDataParam,
							ccUsers: {
								ccUsers: ccUsers
							},
							info: {
								key: $("#processKey").val(),
								processNodeId: $("#processNodeId").val(),
								processInstanceId: $("#processInstanceId").val(),
								taskId: $("#taskId").val(),
								busenessId: busenessId,
								action: btnName,
								assignUserId: $("#assign-UserId").val()
							}
						};


						return data;

					}

				}
			}
		}
	}

	exports.goBack=function(i){
		return_prepage(i);
	};
	function showMask() {
		$('.Mask').show();
		$('#maskText').show();
	}

	function hideMask() {
		$('.Mask').hide();
		$('#maskText').hide();
	}
	//解决安卓部分版本无法打开图片bug，使用原生，判断是否是安卓
	function appCheck(){
		var u = navigator.userAgent, app = navigator.appVersion;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
		//var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		return isAndroid;
	}
	function return_prepage(i){
		try{
			if(appCheck()){
				if(window.js && window.js.goBackCompact){
					window.js.goBackCompact(i);
				}else{
					history.go(-i);
				}
			}else{
				gotoIosFun();
			}
		}catch(error){
			history.go(-i);
		}

	}
	//添加其他需要的属性
	function addPro(data) {
		$.each(data, function(key, val) {
			var operType = $("#tab-" + key).val();
			data[key].operType = operType;
		});
	}
	//删除 object
	function deleteObj(obj) {
		$.each(obj, function(key, val) {
			delete obj[key];
		});
	}
	//封装数据
	function packageData(id, val, data) {
		var table = id.split('-')[0];
		var name = id.split('-')[1];
		if (!data[table]) {
			data[table] = {};
		}
		data[table].tab = table;
		if (!data[table].data) {
			data[table].data = {};
		}
		data[table].data[name] = val;
	}
	//验证数据是否合法 合法则封装数据
	function chechInput(ins, data) {
		var actcont = require("oa/act/js/actcont.js");
		var ok = true;
		$.each(ins, function(i, input) {
			ok = false;
			var opt = eval('(' + $(input).attr('options') + ')');
			var isRequire = opt.isRequire;
			var colNameCh = opt.colNameCh;
			var checkType = opt.checkType;
			var showType = opt.showType
			var id = opt.id;
			var val = "";
			if (showType == actcont.showType.FORM_SHOWTYPE_TEXTAREA) {
				val = $("textarea[name='" + id + "']").val();
			} else {
				val = $("input[name='" + id + "']").val();
			}
			if (!checkVal(isRequire, id, checkType, val)) {
				deleteObj(data);
				if (isRequire == '0' && (checkType == '' || checkType == 'notNull')) {
					//alert(colNameCh+"不能为空！");
					$(".error").text(colNameCh + "不能为空！").show().delay(2000).hide(0);
					return false;
				} else if (checkType == 'positive' && parseFloat(val) <= 0) {
					$(".error").text(colNameCh + "必须大于0！").show().delay(2000).hide(0);
					return false;
				}
			}
			if (val && val.length > 200) {
				$(".error").text("输入的数据不能大于200个字符！").show().delay(2000).hide(0);
				return false;
			}
			ok = true;
			packageData(id, val, data);
		});
		return ok;
	}
	//根据是否必填，以及验证类型校验数据是否合法  合法返回true
	function checkVal(isRequire, id, checkType, val) {
		if (checkType && checkType != '') {
			if (check.check(id, checkType, val)) {
				return true;
			} else {
				return false;
			}
		}
		if (isRequire == '0') {
			if (!(checkType && checkType != '')) {
				if (check.check(id, 'notNull', val)) {
					return true;
				} else {
					return false;
				}
			}
		}
		return true;
	}
	//检查子数据 并封装
	function checkTable(tableId, subs, tab) {
		var actcont = require("oa/act/js/actcont.js");
		if (!subs[tableId]) {
			subs[tableId] = {};
		}
		if (subs[tableId].tab == undefined || subs[tableId].tab != tableId) {
			subs[tableId].tab = tableId;
			subs[tableId].data = [];
		}
		var len = subs[tableId].data.length;
		var ok = false;
		//		var tabs = $("table[name="+tableId+"]");
		//		if(tabs&&tabs.length==0){
		//			ok=true;
		//		}
		//		$.each(tabs,function(i,tab){
		var col = false;
		ok = false;
		var inputs = $(tab).find(".tab-text");
		var rowData = {};
		$.each(inputs, function(i, input) {
			ok = false;
			var opt = eval('(' + $(input).attr('options') + ')');
			var isRequire = opt.isRequire;
			var colNameCh = opt.colNameCh;
			var checkType = opt.checkType;
			var showType = opt.showType
			var id = opt.id;
			var val = "";
			if (showType == actcont.showType.FORM_SHOWTYPE_TEXTAREA) {
				val = $("textarea[name='" + id + "']").val();
			} else {
				val = $("input[name='" + id + "']").val();
			}
			if (!checkVal(isRequire, id, checkType, val)) {
				if (isRequire == '0' || checkType == 'notNull') {
					//alert("第"+(len+1)+"个明细的"+colNameCh+"不能为空！");
					$(".error").text("第" + (len + 1) + "个明细的" + colNameCh + "不能为空！").show().delay(2000).hide(0);
				} else if (checkType == 'positive' && parseFloat(val) <= 0) {
					$(".error").text("第" + (len + 1) + "个明细的" + colNameCh + "不能小于0！").show().delay(2000).hide(0);
				} else {
					//alert("第"+(len+1)+"个明细的"+colNameCh+"数据不合法！");
					$(".error").text("第" + (len + 1) + "个明细的" + colNameCh + "数据不合法！").show().delay(2000).hide(0);
				}
				deleteObj(subs);
				col = false;
				return false;
			} else {
				if (showType == actcont.showType.FORM_SHOWTYPE_INTEGER || showType == actcont.showType.FORM_SHOWTYPE_NUMBER) {
					rowData[opt.colName] = Number(val);
				} else {
					rowData[opt.colName] = val;
				}
				col = true;
			}

		});
		if (!col) {
			return false;
		} else {
			subs[tableId].data.push(rowData);
			ok = true;
		}

		return ok;
	}
	//组合流程参数
	function getProcessCols(data, inputId) {
		if (data == null || data == "") {
			return data;
		}
		var isProcess = $("#" + inputId).val();
		if (isProcess != null && isProcess.length > 0) {
			isProcess = isProcess.substr(0, isProcess.length - 1);
			var is = isProcess.split(",");
			for (var i = 0; i < is.length; i++) {
				var tab = is[i].split('-');
				if (tab.length >= 2) {
					if (typeof(data['' + tab[0] + '']) != 'undefined') {
						if (typeof(data['' + tab[0] + ''].cols) == 'undefined') {
							data['' + tab[0] + ''].cols = [];
						}
						data['' + tab[0] + ''].cols.push(tab[1]);
					}
				}
			}
			return data;
		} else {
			return data;
		}
	}
});
function gotoIosFun() {
	window.onerror = function(err) {
	}
	function connectWebViewJavascriptBridge(callback) {
		if (window.WebViewJavascriptBridge) {
			callback(WebViewJavascriptBridge)
		} else {
			document.addEventListener('WebViewJavascriptBridgeReady', function() {
				callback(WebViewJavascriptBridge)
			}, false)
		}
	}
	connectWebViewJavascriptBridge(function(bridge) {
		bridge.init(function(message, responseCallback) {
		})
		var data = 'goBack';
		bridge.send(data, function(responseData) {
		});
	})
}